package com.zcs.android.app.jetpack.ui.login.model

import android.content.Context
import android.content.Intent
import android.text.Editable
import android.view.View
import android.widget.Toast
import androidx.lifecycle.MutableLiveData
import com.zcs.android.app.jetpack.model.BaseConstant
import com.zcs.android.app.jetpack.ui.HomeActivity
import com.zcs.android.lib.base.SimpleWatcher
import com.zcs.android.lib.utils.LogUtils

/**
 * Created by ZengCS on 2021/4/27.
 * E-mail:zengcs@vip.qq.com
 * Add:中国成都
 * Desc:类说明
 */
class LoginModel(var context: Context) {
    val userName = MutableLiveData("")
    val password = MutableLiveData("")
    val enable = MutableLiveData(false)

    val nameWatcher = object : SimpleWatcher() {
        override fun afterTextChanged(s: Editable) {
            super.afterTextChanged(s)
            userName.value = s.toString()
            judgeEnable()
        }
    }

    val pwdWatcher = object : SimpleWatcher() {
        override fun afterTextChanged(s: Editable) {
            super.afterTextChanged(s)
            password.value = s.toString()
            judgeEnable()
        }
    }

    private fun judgeEnable() {
        enable.value = userName.value!!.isNotEmpty() && password.value!!.isNotEmpty()
        LogUtils.d("enable.value = ${enable.value}")
    }

    fun login(view: View) {
        if (userName.value.equals(BaseConstant.USER_NAME) && password.value
                .equals(BaseConstant.USER_PWD)
        ) {
            Toast.makeText(context, "账号密码正确", Toast.LENGTH_SHORT).show()
            val intent = Intent(context, HomeActivity::class.java)
            context.startActivity(intent)
        } else {
            Toast.makeText(context, "用户名或密码错误", Toast.LENGTH_SHORT).show()
        }
    }
}